/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 main_interface.cc                                         VLC Gtkmmm gui module
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 Copyright © 2006-2008 the VideoLAN team

 Authors: David GUGLIELMI <raxxx.fr@gmail.com>
          David LARROQUE <david.larroque@gmail.com>

 This program is free software; you can redistribute it and/or modify it under
 the terms of the GNU General Public License as published by the Free Software
 Foundation; either version 2 of the License, or (at your option) any later
 version.

 This program is distributed in the hope that it will be useful, but WITHOUT ANY
 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
 PARTICULAR PURPOSE. See the GNU General Public License for more details.

 You should have received a copy of the GNU General Public License along with
 this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
 Street, Fifth Floor, Boston MA 02110-1301, USA.
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/

#include <gtkmm/box.h>
#include <gtkmm/main.h>

#include "main_interface.hh"
#include "menus.hh"

#include "dialogs_provider.hh"
#include "input_manager.hh"

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
static int popup_menu_cb (vlc_object_t *p_this, const char *psz_variable,\
                         vlc_value_t old_val, vlc_value_t new_val, void *param);
static int intf_show_cb (vlc_object_t *p_this, const char *psz_variable,\
                          vlc_value_t old_val, vlc_value_t new_val, void *param);
static int interact_callback (vlc_object_t *, const char *, vlc_value_t,\
                              vlc_value_t, void *);
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
MainInterface::MainInterface (intf_thread_t *_p_intf) : p_intf (_p_intf)
{

  set_position (Gtk::WIN_POS_CENTER);
  set_vlc_window_title ();

  Gtk::VBox *vbox = new Gtk::VBox ();

  // add menu bar
  vbox->pack_start (*Gtk::manage (GVLCMenu::create_menu_bar (this, p_intf)),\
                    false, false);

  add (*Gtk::manage (vbox));

  show_all ();

  signal_hide ().connect (sigc::ptr_fun (&Gtk::Main::quit));

  //Input Manager
  MainInputManager::getInstance (p_intf);

  // TODO code connections

  // Callbacks
  var_Create (p_intf, "interaction", VLC_VAR_ADDRESS);
  var_AddCallback (p_intf, "interaction", interact_callback, this);
  p_intf->b_interaction = true;

  var_AddCallback (p_intf->p_libvlc, "intf-show", intf_show_cb, p_intf);

  // Register callback for the intf-popupmenu variable
  var_AddCallback (p_intf->p_libvlc, "intf-popupmenu", popup_menu_cb, p_intf);

}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
MainInterface::~MainInterface ()
{

  msg_Dbg (p_intf, "Destroying the main interface");
  // TODO save playlist

  // FIXME Temporary exit code
  vlc_object_kill (p_intf->p_libvlc);

  // TODO del callbacks

  p_intf->p_sys->p_mi = NULL;
  
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void 
MainInterface::set_vlc_window_title (Glib::ustring title)
{

  if (title.empty ())
    set_title (_("VLC media player"));
  else
    set_title (title + " - " + _("VLC media player"));

}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// TODO
// TODO
// TODO
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void
MainInterface::request_layout_update ()
{
  // FIXME emit askUpdate();
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// TODO
// TODO
// TODO
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Callbacks
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
static int
interact_callback (vlc_object_t *p_this, const char *psz_var,\
                   vlc_value_t old_val, vlc_value_t new_val, void *param)
{
  intf_dialog_args_t *p_arg = new intf_dialog_args_t;
  p_arg->p_dialog = (interaction_dialog_t *)(new_val.p_address);
  // TODO Dialog event
  return VLC_SUCCESS;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// PopupMenuCB: callback triggered by the intf-popupmenu playlist variable.
// We don't show the menu directly here because we don't want the caller to
// block for a too long time.
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
static int
popup_menu_cb (vlc_object_t *p_this, const char *psz_variable,\
               vlc_value_t old_val, vlc_value_t new_val, void *param)
{
  intf_thread_t *p_intf = (intf_thread_t *)param;

  if (p_intf->pf_show_dialog)
  {
    p_intf->pf_show_dialog (p_intf, INTF_DIALOG_POPUPMENU, new_val.b_bool, 0);
  }

  return VLC_SUCCESS;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// IntfShowCB: callback triggered by the intf-show libvlc variable
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
static int
intf_show_cb (vlc_object_t *p_this, const char *psz_variable,\
              vlc_value_t old_val, vlc_value_t new_val, void *param)
{
  intf_thread_t *p_intf = (intf_thread_t *)param;
  p_intf->p_sys->p_mi->request_layout_update ();

  return VLC_SUCCESS;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
